package com.mxx.basic.app;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

import com.mxx.basic.activity.ActivityCallback;
import com.mxx.basic.utils.ActivityUtil;
import com.mxx.basic.utils.Logger;

/**
 * 作者：刘江飞
 * 时间：2020/8/5  8:46
 * 邮箱：642371742Z@qq.com
 * 描述：Application的基类
 **/
public class BaseApplication extends MultiDexApplication {
    @SuppressLint("StaticFieldLeak")
    private static BaseApplication mBaseApplication;
    private Context context;
    private Application.ActivityLifecycleCallbacks callbacks;
    private Logger mLogger;

    public static BaseApplication getInstance() {
        return mBaseApplication;
    }

    public Context getContext() {
        return context;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        mBaseApplication = this;
        context = base;
        mLogger = Logger.getInstance();
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initActivityCallbacks();

    }

    private void initActivityCallbacks() {
        callbacks = new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
                mLogger.i(activity.getLocalClassName(), "onActivityCreated");
                ActivityUtil.getInstance().pushActivity(activity);
                if (activity instanceof ActivityCallback) {
                    ((ActivityCallback) activity).getTargetActivity();
                    ((ActivityCallback) activity).setTag(activity.getLocalClassName());
                    ((ActivityCallback) activity).removeActionBar(activity);
                    activity.setContentView(((ActivityCallback) activity).getLayoutResId());
                    ((ActivityCallback) activity).setStatusBarColor(activity);
                    ((ActivityCallback) activity).initView();
                    ((ActivityCallback) activity).initData();
                    ((ActivityCallback) activity).initListener();

                }
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                mLogger.i(activity.getLocalClassName(), "onActivityStarted");
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                mLogger.i(activity.getLocalClassName(), "onActivityResumed");
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
                mLogger.i(activity.getLocalClassName(), "onActivityPaused");
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                mLogger.i(activity.getLocalClassName(), "onActivityStopped");
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
                mLogger.i(activity.getLocalClassName(), "onActivitySaveInstanceState");
            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                mLogger.i(activity.getLocalClassName(), "onActivityDestroyed");
                ActivityUtil.getInstance().pushActivity(activity);
            }
        };
        registerActivityLifecycleCallbacks(callbacks);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        if (callbacks != null) {
            unregisterActivityLifecycleCallbacks(callbacks);
        }
    }
}
